pacman::p_load(ggsignif, dplyr, magrittr, reshape, ggplot2, lazyopt)

pnd <- function(test) {
  list <- list()
  ll <- test %$% unique(group) %>% as.character()
  l <- ll %>% length()
  for (i in 1:(l - 1)) {
    for (k in 1:(l - i)) {
      list[[length(list) + 1]] <- c(ll[i], ll[l + 1 - k])
    }
  }
  return(list)
}

pp <- function(test, opt) {
  xlab_style <- lazyopt::fenge(opt$xlab_style)
  ylab_style <- lazyopt::fenge(opt$ylab_style)
  main_style <- lazyopt::fenge(opt$main_style)
  xtext_style <- lazyopt::fenge(opt$xtext_style)
  ytext_style <- lazyopt::fenge(opt$ytext_style)
  x_text_positon <- lazyopt::fenge(opt$x_text_positon) %>% as.numeric()
  imageSize <- lazyopt::fenge(opt$imageSize)
  resolution <- match.arg(opt$resolution %>% as.character(), c("72", "96", "300", "600")) %>% as.numeric()
  width <- NA; height <- NA
  if (imageSize[1] != "NULL") { width <- imageSize[1] %>% as.numeric() }
  if (imageSize[2] != "NULL") { height <- imageSize[2] %>% as.numeric() }
  (
    ggplot(test, aes(group, exp, fill = group)) +
      geom_boxplot(width = 0.5, outlier.size = 2,varwidth = TRUE) +
      theme(plot.title = element_text(size = 25), axis.text.x = element_text(size = 15, angle = 0), axis.text.y = element_text(size = 15), axis.title.x = element_text(size = 23), axis.title.y = element_text(size = 23)) +
      labs(x = 'Gene', y = 'Expression') +
      geom_signif(comparisons = pnd(test),
                  step_increase = 0.1, map_signif_level = T, test = t.test, show.legend = T) +
      geom_signif(comparisons = pnd(test),
                  step_increase = 0.1, map_signif_level = F, test = t.test, show.legend = F, vjust = 2) +
      theme_bw() +
      theme(
        panel.border = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        legend.background = element_blank(),
        axis.text.y = element_text(family = ytext_style[1], face = ytext_style[2], size = as.numeric(ytext_style[3])),
        axis.text.x = element_text(family = xtext_style[1], face = xtext_style[2], size = as.numeric(xtext_style[3]), angle = x_text_positon[1], hjust = x_text_positon[2]),
        axis.title.x = element_text(size = as.numeric(xlab_style[3]), family = xlab_style[1], face = xlab_style[2], colour = xlab_style[5]),
        axis.title.y = element_text(size = as.numeric(ylab_style[3]), family = ylab_style[1], face = ylab_style[2], colour = ylab_style[5]),
        title = element_text(size = as.numeric(main_style[3]), family = main_style[1], face = main_style[2], main_style[5])
      ) +
      labs(title = main_style[4], y = ylab_style[4], x = xlab_style[4]) +
      scale_fill_manual(values = colorRampPalette(opt %$% lazyopt::fenge(colorstyle))(test$group %>% unique() %>% length()))
  ) %>% ggsave(
    paste0(opt$filepath, "/", opt$imageName, ".", opt$imageformt), ., width = width, height = height, dpi = resolution
  )
}

zz <- function(opt) {
  return(
    opt %$%
      read.delim(tablepath) %>%
      melt() %>%
      select(2, 1) %>%
      set_colnames(c("exp", "group"))
  )
}

arg <- c("-i", "E:/projects/R_draw_sc/test/xianzhuxingboxplot/data/Diff.mat.txt",
         "-o", "E:/projects/R_draw_sc/test/xianzhuxingboxplot/image")


matrix(c(
  "tablepath", "i", 2, "character", "The path to the table data read", "",
  "filepath", "o", 2, "character", "The package path of the output image", "",
  "colorstyle", "cs", 1, "character", "Set the color to ':' split", "#CD0000:#3A89CC:#769C30:#D99536:#7B0078:#BFBC3B:#E2609F:#00688B:#C10077:#CAAA76:#EEEE00:#458B00:#8B4513:#008B8B:#6E8B3D:#8B7D6B:#7FFF00:#CDBA96:#ADFF2F",
  "main_style", "ms", 1, "character", "Main style Font:font type:font size:name", "sans:bold.italic:16: :black",
  "xtext_style", "xts", 1, "character", "X text style Font:font type:font size", "sans:bold.italic:11",
  "x_text_positon", "xtp", 1, "character", "split by ':', sample angle:hjust", "60:1",
  "ytext_style", "yts", 1, "character", "Y text style Font:font type:font size", "sans:bold.italic:11",
  "xlab_style", "xls", 1, "character", "X lab style Font:font type:font size:name:color", "sans:bold.italic:14: :black",
  "ylab_style", "yls", 1, "character", "Y lab style Font:font type:font size:name:color", "sans:bold.italic:14: :black",
  "imageSize", "is", 1, "character", "The height and width of the picture", "NULL:NULL",
  "resolution", "dpi", 1, "numeric", "Set the resolution to allow 72,96,300 or 600", "300",
  "imageformt", "if", 1, "character", "pdf,tiff,png", "png",
  "imageName", "in", 1, "character", "picture name", "boxplot_p",
  "help", "h", 0, "numeric", "Help document", "1"
), byrow = TRUE, ncol = 6) %>%
  lazyopt(arg) %>%
  pp(zz(.), .)







